## Core cbv field-related stuff
from wjContact.views.fields import FieldList 

## Contact specific cbv field-related stuff
from wjContact.contacts.views.widgets import PhoneWidget

from django import newforms as forms
from wjContact.contacts.models import Phone

class PhoneField(forms.MultiValueField):
    """ 
    This class groups together the fields required for entering newphone
    details.
    """
            
    def __init__(self, *args, **kwargs):
        fields = (
            forms.CharField(required=False, max_length=20, initial='new_entry'),
            forms.CharField(max_length=15, initial='111-111-1111'),
            forms.ChoiceField(choices=Phone.PHONE_CHOICES, initial="mobile"),
            forms.BooleanField(required=False, help_text="Check this to delete.")
            )
        super(PhoneField, self).__init__(fields, *args, **kwargs)


    def compress(self, data_list):       
        if data_list:
            ## Some items may not be strings, ie the phone_delete checkbox 
            ## returns a boolean True or False, so unicoderise them all here to
            ## be on the safe side.
            data_list=[unicode(item) for item in data_list]
            ## Join on hash sign, or anything *except* a dash (-).
            ## This is because the phone-number field may have
            ## a value with a dash in it, ie 111-111-1111. So it's ok when
            ## you join on a dash and get something like 
            ## 111-111-1111-mobile-neutral, but then what happens when you 
            ## want to split it again?
            return '#'.join(data_list)
        raise Exception, "No data_list passed into a PhoneField.compress() method!"

 
##============================================================================##
    
